package cn.hxzy.servlet.admin;

import cn.hxzy.entity.News;
import cn.hxzy.service.NewsService;
import cn.hxzy.util.Md5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/admin/editNews")
public class EditNewsServlet extends HttpServlet {
    NewsService newsService = new NewsService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            News news= newsService.findByNewsNumber(Integer.parseInt(id));
            req.setAttribute("news", news);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/editNews.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String newValue = req.getParameter("newValue");
            System.out.println(id);
            System.out.println(name);
            System.out.println(newValue);
            if (id != null && id != "") {
                News news = new News(name,newValue,new Date(),1,Integer.parseInt(id));
//                news.setId(Integer.parseInt(id));
                System.out.println(news);
                newsService.update(news);
            }
            resp.sendRedirect("showNews");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
